<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="NodeVS插件Node Debug This extension is bundled with Visual Studio Code and together with Node Debug (legacy) forms the Node.js debugging experience. Node Debug is the debugger for Node.js versions &gt;&#x3D;">
<meta property="og:type" content="article">
<meta property="og:title" content="node">
<meta property="og:url" content="http://yoursite.com/2020/04/30/%E5%89%8D%E7%AB%AF/Node.js/node/index.html">
<meta property="og:site_name" content="heweiliang blog">
<meta property="og:description" content="NodeVS插件Node Debug This extension is bundled with Visual Studio Code and together with Node Debug (legacy) forms the Node.js debugging experience. Node Debug is the debugger for Node.js versions &gt;&#x3D;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2020/05/01/XChWwNZLqicjGER.png">
<meta property="og:image" content="https://i.loli.net/2020/05/01/1ZiocFOTmGEKw4Q.png">
<meta property="article:published_time" content="2020-04-30T02:15:37.860Z">
<meta property="article:modified_time" content="2020-06-27T03:04:24.707Z">
<meta property="article:author" content="heweiliang">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="node-">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2020/05/01/XChWwNZLqicjGER.png">

<link rel="canonical" href="http://yoursite.com/2020/04/30/%E5%89%8D%E7%AB%AF/Node.js/node/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>node | heweiliang blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="heweiliang blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">heweiliang blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/yourname" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/30/%E5%89%8D%E7%AB%AF/Node.js/node/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="heweiliang">
      <meta itemprop="description" content="heweiliang blog">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="heweiliang blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          node<a href="https://github.com/user-name/repo-name/tree/branch-name/subdirectory-name_posts/%E5%89%8D%E7%AB%AF/Node.js/node.md" class="post-edit-link" title="编辑" rel="noopener" target="_blank"><i class="fa fa-pencil"></i></a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-30 10:15:37" itemprop="dateCreated datePublished" datetime="2020-04-30T10:15:37+08:00">2020-04-30</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-27 11:04:24" itemprop="dateModified" datetime="2020-06-27T11:04:24+08:00">2020-06-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/node/" itemprop="url" rel="index"><span itemprop="name">node</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>30k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>27 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Node"><a href="#Node" class="headerlink" title="Node"></a>Node</h1><h2 id="VS插件"><a href="#VS插件" class="headerlink" title="VS插件"></a>VS插件</h2><h3 id="Node-Debug"><a href="#Node-Debug" class="headerlink" title="Node Debug"></a>Node Debug</h3><blockquote>
<p>This extension is bundled with Visual Studio Code and together with <strong>Node Debug (legacy)</strong> forms the <a href="https://nodejs.org/" target="_blank" rel="noopener">Node.js</a> debugging experience.</p>
<p><strong>Node Debug</strong> is the debugger for Node.js versions &gt;= 8.0.</p>
<p>See a general overview of debugging in VS Code <a href="https://code.visualstudio.com/docs/editor/debugging" target="_blank" rel="noopener">here</a>.</p>
<p>Documentation for Node.js specific debugging can be found <a href="https://code.visualstudio.com/docs/nodejs/nodejs-debugging" target="_blank" rel="noopener">here</a>.</p>
<p>Please submit bugs and feature requests to the <a href="https://github.com/microsoft/vscode/issues" target="_blank" rel="noopener">VS Code repository</a>.</p>
<p> License</p>
<p>Copyright (c) Microsoft Corporation. All rights reserved.</p>
<p>Licensed under the <a href="https://github.com/Microsoft/vscode-node-debug2/blob/master/LICENSE.txt" target="_blank" rel="noopener">MIT</a> License.</p>
</blockquote>
<h3 id="Search-node-modules"><a href="#Search-node-modules" class="headerlink" title="Search node_modules"></a>Search node_modules</h3><blockquote>
<p>Simple plugin for VS Code that allows you to quickly navigate the file inside your project’s <code>node_modules</code> directory.</p>
<p>Chances are you have the <code>node_modules</code> folder excluded from the built-in search in VS Code, which means if you want to open and/or edit a file inside <code>node_modules</code>, you can have to find it manually, which can be annoying when your <code>node_modules</code> folder is large.</p>
<p>Features</p>
<ul>
<li><p>Quickly navigate and open files in <code>node_modules</code> by traversing the folder tree.</p>
<p>Settings</p>
</li>
<li><p><code>search-node-modules.useLastFolder</code>: Default to folder of last opened file when searching (defaults to <code>false</code>).</p>
</li>
<li><p><code>search-node-modules.path</code>: Relative path to node_modules folder (defaults to <code>node_modules</code>).</p>
</li>
</ul>
<p>Extension Packs</p>
<ul>
<li><a href="https://marketplace.visualstudio.com/items?itemName=waderyan.nodejs-extension-pack" target="_blank" rel="noopener">Node.js Extension Pack</a></li>
</ul>
<p>Links</p>
<ul>
<li>Visual Studio Marketplace: <a href="https://marketplace.visualstudio.com/items?itemName=jasonnutter.search-node-modules" target="_blank" rel="noopener">https://marketplace.visualstudio.com/items?itemName=jasonnutter.search-node-modules</a></li>
<li>Repo: <a href="https://github.com/jasonnutter/vscode-search-node-modules" target="_blank" rel="noopener">https://github.com/jasonnutter/vscode-search-node-modules</a></li>
<li>Known Issues: <a href="https://github.com/jasonnutter/vscode-search-node-modules/issues" target="_blank" rel="noopener">https://github.com/jasonnutter/vscode-search-node-modules/issues</a></li>
<li>Change Log: <a href="https://github.com/jasonnutter/vscode-search-node-modules/blob/master/CHANGELOG.md" target="_blank" rel="noopener">https://github.com/jasonnutter/vscode-search-node-modules/blob/master/CHANGELOG.md</a></li>
</ul>
</blockquote>
<h3 id="Node-Exec"><a href="#Node-Exec" class="headerlink" title="Node Exec"></a>Node Exec</h3><h3 id="Node-Snippets"><a href="#Node-Snippets" class="headerlink" title="Node Snippets"></a>Node Snippets</h3><blockquote>
<p>Features</p>
<p>A collection of NodeJS snippets</p>
<p>The following commands are available:</p>
<ul>
<li><code>node-express</code>, creates an express server</li>
<li><code>node-express-get</code>, creates GET route</li>
<li><code>node-express-get-params</code>, creates a GET route and shows how to access parameters</li>
<li><code>node-express-post</code>, creates a POST route</li>
<li><code>node-express-post-params</code>, creates a POST route and shows how to access the body</li>
<li><code>node-express-post-params-alt</code>, creates a POST route, shows how to access the body, works for express 4.16 and above</li>
<li><code>node-express-middleware-logger</code>, creates an example middleware</li>
<li><code>node-express-middleware-error</code>, creates an error handling middleware</li>
<li><code>node-http-server</code>, creates a simple HTTP server</li>
<li><code>node-file-read-sync</code>, reads a file synchronously</li>
<li><code>node-file-read-async</code>, reads a file asynchronously, with a callback</li>
<li><code>node-event-emitter</code>, creates an event emitter, emit events and shows to subscribe to said event</li>
<li><code>node-promise-create</code>, creates a Promise</li>
<li><code>node-promise-shorthand</code>, creates a Promises using the static methods <code>resolve()</code> and <code>reject()</code></li>
<li><code>node-promise-all</code>, resolves a list of Promises using the <code>Promise.all([])</code> method</li>
<li><code>node-async-await</code>, using async/await</li>
<li><code>node-express-schema-validation</code>, adding schema validation for express, read more about the usage of schema validation with <code>Joi</code> at <a href="https://github.com/hapijs/joi" target="_blank" rel="noopener">https://github.com/hapijs/joi</a></li>
</ul>
</blockquote>
<h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><p>Node.js可以用来做什么？</p>
<ul>
<li>具有复杂逻辑的动态网站 </li>
<li>WebSocket服务器 </li>
<li>命令行工具 </li>
<li>带有图形界面的本地应用程序 </li>
<li>……</li>
</ul>
<table>
<thead>
<tr>
<th>浏览器</th>
<th>内核</th>
</tr>
</thead>
<tbody><tr>
<td>IE</td>
<td>Trident</td>
</tr>
<tr>
<td>FireFox</td>
<td>Gecko</td>
</tr>
<tr>
<td>Chrome</td>
<td>WebKit</td>
</tr>
<tr>
<td>Safari</td>
<td>WebKit</td>
</tr>
<tr>
<td>Opera</td>
<td>Presto</td>
</tr>
<tr>
<td>Edge</td>
<td>Chakra</td>
</tr>
</tbody></table>
<blockquote>
<p>Node.js 是一种建立在Google Chrome’s v8 engine上的 non-blocking (非阻塞）, event-driven （基于事件的） I/O平台.<br>Node.js平台使用的开发语言是JavaScript，平台提供了操作系统低层的API，方便做服务器端编程，具体包括文件操作、进程操作、通信操作等系统模块</p>
</blockquote>
<h3 id="cmd命令"><a href="#cmd命令" class="headerlink" title="cmd命令"></a>cmd命令</h3><ul>
<li>新建一个指定的文件 名称</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> on  &gt;a.txt</span><br><span class="line"><span class="built_in">echo</span> <span class="number">123</span> &gt;a.txt</span><br><span class="line">touch <span class="number">124</span>.qdfs</span><br></pre></td></tr></table></figure>

<ul>
<li>删除文件 del 文件名</li>
<li>查看文件 cat  文件名</li>
<li>写入文件</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="number">123</span>asdfsadfdsf &gt;&gt; a.txt</span><br><span class="line">cat &gt;&gt; a.txt</span><br><span class="line">输入内容</span><br></pre></td></tr></table></figure>



<ul>
<li>notepad 打开记事本</li>
<li>mspaint 打开画图</li>
<li>calc 打开计算机</li>
<li>write 写字板</li>
<li>sysdm.cpl 打开环境变量设置窗口</li>
<li>md 创建目录</li>
<li>rmdir(rd) 删除目录，目录内没有文档。</li>
<li>rd /s/q 目录名 包含文件夹下的子目录</li>
<li>echo on a.txt 创建空文件</li>
<li>del 删除文件</li>
<li>rm 文件名 删除文件</li>
<li>cat 文件名 查看文件内容</li>
<li>cat &gt; 文件名 向文件中写上内容。</li>
</ul>
<h3 id="node-js-安装"><a href="#node-js-安装" class="headerlink" title="node.js 安装"></a>node.js 安装</h3><ul>
<li>第一种直接安装</li>
<li>第二种nvm node版本管理工具</li>
</ul>
<h4 id="settings-txt文件的配置"><a href="#settings-txt文件的配置" class="headerlink" title="settings.txt文件的配置"></a>settings.txt文件的配置</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">root: D:\SoftWare\NodeJs\nvm &#x2F;&#x2F; nvm node版本控制的软件的位置</span><br><span class="line">path: D:\SoftWare\NodeJs\nodejs &#x2F;&#x2F;当前使用的node的位置</span><br><span class="line">arch: 64 </span><br><span class="line">proxy: none</span><br><span class="line">node_mirror: http:&#x2F;&#x2F;npm.taobao.org&#x2F;mirrors&#x2F;node&#x2F;</span><br><span class="line">npm_mirror: https:&#x2F;&#x2F;npm.taobao.org&#x2F;mirrors&#x2F;npm&#x2F;</span><br></pre></td></tr></table></figure>

<h4 id="环境变量的配置"><a href="#环境变量的配置" class="headerlink" title="环境变量的配置"></a>环境变量的配置</h4><p>配置nvm和Node.js环境变量</p>
<ul>
<li>NVM_HOME:D:\SoftWare\NodeJs\nvm </li>
<li>NVM_SYMLINK: D:\SoftWare\NodeJs\nodejs </li>
</ul>
<blockquote>
<p>nvm不能安装在programs files，因为空格问题，非常不爽<br>exit status 1: ‘C:\Program’ �����ڲ����ⲿ���Ҳ���ǿ����еĳ���<br>���������ļ���</p>
<p>可以通过在nvm根目录将program files替换为 progra<del>1，如下：<br>root: C:\progra</del>1\nvm<br>path: C:\progra~1\nodejs</p>
</blockquote>
<h3 id="nvm常用的命令"><a href="#nvm常用的命令" class="headerlink" title="nvm常用的命令"></a>nvm常用的命令</h3><ul>
<li>nvm list 查看当前安装的Node.js所有版本</li>
<li>nvm install 版本号 安装指定版本的Node.js nvm install latest 最新版本</li>
<li>nvm uninstall 版本号 卸载指定版本的Node.js</li>
<li>nvm use 版本号 选择指定版本的Node.js</li>
</ul>
<h4 id="node-js版本命名规则"><a href="#node-js版本命名规则" class="headerlink" title="node.js版本命名规则"></a>node.js版本命名规则</h4><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>浏览器的内核包括两部分核心：</p>
<ol>
<li>DOM渲染引擎</li>
<li>JS解析引擎</li>
</ol>
<p>node.js使JavaScript不只是运行在浏览器</p>
<p>js运行在浏览器中的内核中的js引擎内部</p>
<p>基于Node.js 和第三方工具electron可以开发桌面应用程序</p>
<p>REPL read-eval-print-loop 读取代码-执行-打印结果</p>
<p>在REPL 环境中，<code>_</code>表示最后一次执行的结果</p>
<p>.exit 退出REPL 环境</p>
<h2 id="服务器端模块化"><a href="#服务器端模块化" class="headerlink" title="服务器端模块化"></a>服务器端模块化</h2><ul>
<li><p>不存在浏览器的window对象 ==&gt; </p>
<p>全局成员概述 Global Objects</p>
<p> _filename 包含文件名的全路径</p>
<p>_dirname    文件的路径（不包含文件名称）</p>
<p>定时函数</p>
<p>​    var timer = etTimeout(function(){</p>
<p>},1000);</p>
<p>clearTimeout(timer);</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timer = etTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	consloe.log(<span class="string">'sadfasddf'</span>)</span><br><span class="line">&#125;,<span class="number">1000</span>);</span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	clearTimeout(timer);</span><br><span class="line">&#125;,<span class="number">2000</span>)</span><br><span class="line"></span><br><span class="line">global.console.log(<span class="string">'asdfasdfasdf'</span>)</span><br></pre></td></tr></table></figure>





</li>
</ul>
<p>process 进程</p>
<p>process.argv 是一个数组，默认情况下，前两项数据分别是 Node.js 环境的路径 当前执行的js文件的全路径，</p>
<p>从第三个参数起，接收命令行的参数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node 文件,js 123 234234 234</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.log(process.arch)</span><br></pre></td></tr></table></figure>



<p>exports  // modules  require 模块化 </p>
<p>模块化开发</p>
<p>传统非模块化开发缺点</p>
<ol>
<li>命名冲突</li>
<li>文件依赖</li>
</ol>
<p>前端标准的模块化规范 异步</p>
<ol>
<li>AMD -= requiresjs</li>
<li>CMS - seajs</li>
</ol>
<p>服务器端标准的模块化规范 同步</p>
<ol>
<li>CommonJS - Node.js</li>
</ol>
<p>模块化相关的规则</p>
<ol>
<li>如何定义模块：一个js文件就是一个模块，模块内部的成员都是相互独立</li>
<li>模块成员的导入和导出</li>
</ol>
<p>文件加载</p>
<p>导出模块成员</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> </span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//导出模块成员</span></span><br><span class="line">exports.sum = sum;</span><br><span class="line"><span class="comment">//导出模块成员的第二种方式</span></span><br><span class="line"><span class="built_in">module</span>.exports = sum;</span><br><span class="line"><span class="comment">// 第三种方法 很少用</span></span><br><span class="line"><span class="keyword">var</span> flag = <span class="number">100</span>;</span><br><span class="line">global.flag = flag</span><br></pre></td></tr></table></figure>

<p>已经加载的模块会缓存，多次加载的文件会自会加载一次</p>
<p>引入模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F; 当前路径下</span><br><span class="line">var module &#x3D; require(&#39;.&#x2F;03.js&#39;);</span><br><span class="line">var module &#x3D; require(&#39;.&#x2F;03&#39;);</span><br><span class="line"> 后缀可以省略</span><br><span class="line"> 模块文件的后缀3种情况 .js .json</span><br></pre></td></tr></table></figure>



<ul>
<li>服务器端模块化规范CommonJS与实现Node.js</li>
<li>模块导出与引入</li>
<li>模块导出机制分析</li>
<li>模块加载规则<ul>
<li>模块查找 不加扩展名的时候会按照如下后缀顺序进行查找 .js .json .node（C语言编译）</li>
<li></li>
</ul>
</li>
<li>模块分类<ul>
<li>自定义模块</li>
<li>系统核心模块<ul>
<li>fs 文件操作</li>
<li>http 网络操作</li>
<li>path 路径操作</li>
<li>querystring 查询参数解析</li>
<li>url url解析</li>
<li>……</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="ES6常用语法"><a href="#ES6常用语法" class="headerlink" title="ES6常用语法"></a>ES6常用语法</h2><ul>
<li>变量声明let与const</li>
<li>变量的解构赋值<ul>
<li>数组解构赋值</li>
<li>对象解构赋值</li>
<li>字符串解构赋值</li>
</ul>
</li>
<li>字符串扩展<ul>
<li>includes()</li>
<li>startsWith()</li>
<li>endsWith()</li>
<li>模板字符串</li>
</ul>
</li>
<li>函数扩展<ul>
<li>参数默认值</li>
<li>参数结构赋值</li>
<li>rest参数</li>
<li>扩展运算符</li>
<li>箭头函数</li>
</ul>
</li>
<li>类与继承</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Animal</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line">Animal.prototype.showName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Animal(<span class="string">'Tom'</span>);</span><br><span class="line">a.showName();</span><br><span class="line"><span class="keyword">var</span> a1 = <span class="keyword">new</span> Animal(<span class="string">'Jerry'</span>);</span><br><span class="line">a1.showName()</span><br><span class="line"></span><br><span class="line"><span class="comment">// ------------------</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line">    <span class="comment">//静态方法(静态方法只能通过类名调用，不可以使用实例对象调用)</span></span><br><span class="line">    <span class="keyword">static</span> showInfo()&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'hi'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 构造函数</span></span><br><span class="line">    <span class="keyword">constructor</span>(name)&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    showName()&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> a = <span class="keyword">new</span> Animal(<span class="string">'spike'</span>);</span><br><span class="line">a.showName();</span><br><span class="line">Animal.showInfo();</span><br><span class="line"><span class="comment">// 类的继承 extends</span></span><br><span class="line">面向对象的编程语言</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="title">extend</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(name,color)&#123;</span><br><span class="line">       	<span class="keyword">super</span>(name); <span class="comment">//super 用来调用父类 </span></span><br><span class="line">        <span class="keyword">this</span>.color = color;</span><br><span class="line">    &#125;</span><br><span class="line">    showColor()&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.color);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> d = <span class="keyword">new</span> Dog(<span class="string">'gg'</span>,<span class="string">'yellow'</span>);</span><br><span class="line">d.showColor();</span><br><span class="line">d.showName();</span><br><span class="line">Dog.showInfo();</span><br></pre></td></tr></table></figure>





<h2 id="Buffer基本操作"><a href="#Buffer基本操作" class="headerlink" title="Buffer基本操作"></a>Buffer基本操作</h2><blockquote>
<p>Buffer对象是Node<code>处理二进制数据</code>的一个接口。它是Node原生提供的全局对象，可以直接使用，不需要require(‘buffer’)。</p>
</blockquote>
<ul>
<li>实例化 构造方法<ul>
<li>Buffer.from(array)</li>
<li>Buffer.from(string)</li>
<li>Buffer.alloc(size)</li>
</ul>
</li>
<li>功能方法 静态方法<ul>
<li>Buffer.isEncoding() 判断是否支持该编码</li>
<li>Buffer.isBuffer() 判断是否为Buffer</li>
<li>Buffer.byteLength() 返回指定编码的字节长度，默认utf8</li>
<li>Buffer.concat() 将一组Buffer对象合并为一个Buffer对象</li>
</ul>
</li>
<li>实例方法<ul>
<li>write() 向buffer对象中写入内容</li>
<li>slice() 截取新的buffer对象</li>
<li>toString() 把buf对象转成字符串</li>
<li>toJson() 把buf对象转成json形式的字符串</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">Global.Buffer</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//实例化buf对象</span></span><br><span class="line"><span class="keyword">let</span> buf = <span class="keyword">new</span> Buffer(<span class="number">5</span>) <span class="comment">// 不推荐使用</span></span><br><span class="line"><span class="keyword">let</span> buf = Buffer.alloc(<span class="number">5</span>)</span><br><span class="line">conole.log(buf);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 字符=》16 进制</span></span><br><span class="line"><span class="keyword">let</span> buf = Buffer.form(<span class="string">'hello'</span>);</span><br><span class="line"><span class="keyword">let</span> buf = Buffer.form(<span class="string">'hello'</span>，’utf<span class="number">-8</span>‘);<span class="comment">// 默认编码 utf-8</span></span><br><span class="line"><span class="built_in">console</span>.log(buf)</span><br><span class="line"><span class="keyword">let</span> buf = Buffer.form([<span class="number">0x62</span>,<span class="number">0x75</span>]);<span class="comment">// 16进制</span></span><br><span class="line"><span class="built_in">console</span>.log(buf)</span><br><span class="line"><span class="comment">//静态方法</span></span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isEncoding(<span class="string">'utf-8'</span>))</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isEncoding(<span class="string">'base64'</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> buf = Buffer.from(<span class="string">'hello'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isBuffer(buf));</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isBuffer(&#123;&#125;));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> buf = Buffer.from(<span class="string">'hello'</span>);<span class="comment">//默认 utf-8 三个字节 == 一个汉字</span></span><br><span class="line"><span class="built_in">console</span>.log(Buffer.byteLength(<span class="string">'aasdf'</span>));<span class="comment">// 字节长度，不是字符长度</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> buf1 = Buffer.alloc(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">let</span> buf2 = Buffer.alloc(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">let</span> buf3 = Buffer.concat([buf1,buf2]);</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.byteLength(buf3)) <span class="comment">// 8</span></span><br><span class="line"><span class="built_in">console</span>.log(buf3.toString())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">+ write() 向buffer对象中写入内容</span><br><span class="line"></span><br><span class="line">+ toString() 把buf对象转成字符串</span><br><span class="line">+ toJson() 把buf对象转成json形式的字符串</span><br><span class="line"></span><br><span class="line">---------------</span><br><span class="line">实例方法</span><br><span class="line">buf.write(string[, offset[, length]][, encoding])</span><br><span class="line"></span><br><span class="line">write() 向Buffer中写入内容</span><br><span class="line"><span class="keyword">let</span> buf = Buffer.alloc(<span class="number">5</span>);</span><br><span class="line">buf,write(<span class="string">'hello'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(buf)</span><br><span class="line"></span><br><span class="line">buf,write(<span class="string">'hello'</span>,<span class="number">2</span>，<span class="number">2</span>) <span class="comment">//第二个开始写 第三个参数 写入长度多少个字体</span></span><br><span class="line"><span class="built_in">console</span>.log(buf)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">+ slice() 截取新的buffer对象 buf.slice([start[, end]])</span><br><span class="line"><span class="keyword">let</span> buf = Buffer.from(<span class="string">'hello'</span>);</span><br><span class="line"><span class="keyword">let</span> buf1 = buf.slice(<span class="number">2</span>,<span class="number">4</span>);<span class="comment">// 第一个参数 start 2 - 4 end</span></span><br><span class="line"><span class="built_in">console</span>.log(buf === buf1)</span><br><span class="line"><span class="built_in">console</span>.log(buf1.toString())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">toJson() <span class="comment">// 把buf对象转为 不需要显式调用，当JSON .stringify方法调用的时候toJSON方法</span></span><br><span class="line">buf.toJSON()</span><br><span class="line">Returns a <span class="built_in">JSON</span> representation <span class="keyword">of</span> buf. JSON.stringify() implicitly calls <span class="keyword">this</span> <span class="function"><span class="keyword">function</span> <span class="title">when</span> <span class="title">stringifying</span> <span class="title">a</span> <span class="title">Buffer</span> <span class="title">instance</span>.</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">const</span> <span class="title">buf</span> = <span class="title">Buffer</span>.<span class="title">from</span>(<span class="params">[<span class="number">0x1</span>, <span class="number">0x2</span>, <span class="number">0x3</span>, <span class="number">0x4</span>, <span class="number">0x5</span>]</span>);</span></span><br><span class="line"><span class="function"><span class="title">const</span> <span class="title">json</span> = <span class="title">JSON</span>.<span class="title">stringify</span>(<span class="params">buf</span>);</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">console</span>.<span class="title">log</span>(<span class="params">json</span>);</span></span><br><span class="line"><span class="function">// <span class="title">Prints</span>: </span>&#123;<span class="string">"type"</span>:<span class="string">"Buffer"</span>,<span class="string">"data"</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]&#125;</span><br><span class="line"><span class="keyword">const</span> copy = <span class="built_in">JSON</span>.parse(json, (key, value) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> value &amp;&amp; value.type === <span class="string">'Buffer'</span> ?</span><br><span class="line">    Buffer.from(value) :</span><br><span class="line">    value;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(copy);</span><br><span class="line"><span class="comment">// Prints: &lt;Buffer 01 02 03 04 05&gt;</span></span><br></pre></td></tr></table></figure>

<p>Buffer本质上是一个字节数组</p>
<ol>
<li>构造函数</li>
<li>静态方法</li>
<li>实例方法</li>
</ol>
<ul>
<li>JSON</li>
<li>AJAX </li>
<li>正则表达式</li>
<li>同步和异步</li>
</ul>
<h2 id="核心模块API-Buffer-path-fs"><a href="#核心模块API-Buffer-path-fs" class="headerlink" title="核心模块API Buffer // path // fs"></a>核心模块API Buffer // path // fs</h2><h3 id="路径操作"><a href="#路径操作" class="headerlink" title="路径操作"></a>路径操作</h3><ul>
<li>路径基本操作API</li>
</ul>
<p>path</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line">path</span><br><span class="line"><span class="comment">// 路径</span></span><br><span class="line">获取路径的最后一部分</span><br><span class="line">path.basename(path[, ext])</span><br><span class="line"><span class="comment">// linux的</span></span><br><span class="line">path.basename(<span class="string">'/foo/bar/baz/asdf/quux.html'</span>);</span><br><span class="line"><span class="comment">// Returns: 'quux.html'</span></span><br><span class="line"></span><br><span class="line">path.basename(<span class="string">'/foo/bar/baz/asdf/quux.html'</span>, <span class="string">'.html'</span>);</span><br><span class="line"><span class="comment">// Returns: 'quux'</span></span><br><span class="line">获取路径</span><br><span class="line"><span class="built_in">console</span>.log(__dirname);</span><br><span class="line"><span class="built_in">console</span>.log(path.dirname(<span class="string">'/foo/bar/baz/asdf/quux.html'</span>))</span><br><span class="line">获取扩展名称</span><br><span class="line">path.extname(path)</span><br><span class="line">path.extname(<span class="string">'index.html'</span>);</span><br><span class="line"><span class="comment">// Returns: '.html'</span></span><br><span class="line"></span><br><span class="line">path.extname(<span class="string">'index.coffee.md'</span>);</span><br><span class="line"><span class="comment">// Returns: '.md'</span></span><br><span class="line"></span><br><span class="line">path.extname(<span class="string">'index.'</span>);</span><br><span class="line"><span class="comment">// Returns: '.'</span></span><br><span class="line"></span><br><span class="line">path.extname(<span class="string">'index'</span>);</span><br><span class="line"><span class="comment">// Returns: ''</span></span><br><span class="line"></span><br><span class="line">path.extname(<span class="string">'.index'</span>);</span><br><span class="line"><span class="comment">// Returns: ''</span></span><br><span class="line"></span><br><span class="line">path.extname(<span class="string">'.index.md'</span>);</span><br><span class="line"><span class="comment">// Returns: '.md</span></span><br><span class="line"></span><br><span class="line">path.isAbsolute(path)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">路径格式化处理</span><br><span class="line">path.format(pathObject) obj -&gt; string</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> path1 = path.format(&#123;</span><br><span class="line">  dir: <span class="string">'C:\\path\\dir'</span>,</span><br><span class="line">  base: <span class="string">'file.txt'</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(path1)</span><br><span class="line"><span class="comment">// Returns: 'C:\\path\\dir\\file.txt'</span></span><br><span class="line"></span><br><span class="line">path.parse(path) string -&gt; obj</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> obj = path.parse(<span class="string">'C:\\path\\dir\\file.txt'</span>);</span><br><span class="line"><span class="comment">// Returns:</span></span><br><span class="line"><span class="comment">// &#123; root: 'C:\\',</span></span><br><span class="line"><span class="comment">//   dir: 'C:\\path\\dir',</span></span><br><span class="line"><span class="comment">//   base: 'file.txt',</span></span><br><span class="line"><span class="comment">//   ext: '.txt',</span></span><br><span class="line"><span class="comment">//   name: 'file' &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.base);</span><br><span class="line"></span><br><span class="line">path.isAbsolute(path)</span><br><span class="line">path.isAbsolute(<span class="string">'//server'</span>);    <span class="comment">// true</span></span><br><span class="line">path.isAbsolute(<span class="string">'\\\\server'</span>);  <span class="comment">// true</span></span><br><span class="line">path.isAbsolute(<span class="string">'C:/foo/..'</span>);   <span class="comment">// true</span></span><br><span class="line">path.isAbsolute(<span class="string">'C:\\foo\\..'</span>); <span class="comment">// true</span></span><br><span class="line">path.isAbsolute(<span class="string">'bar\\baz'</span>);    <span class="comment">// false</span></span><br><span class="line">path.isAbsolute(<span class="string">'bar/baz'</span>);     <span class="comment">// false</span></span><br><span class="line">path.isAbsolute(<span class="string">'.'</span>);           <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">path.join([...paths]) 拼接路径 ../表示上层路径 .表示当前路径，在连接路径的时候会格式化</span><br><span class="line"></span><br><span class="line">path.join(<span class="string">'/foo'</span>, <span class="string">'bar'</span>, <span class="string">'baz/asdf'</span>, <span class="string">'quux'</span>, <span class="string">'..'</span>);</span><br><span class="line"><span class="comment">// Returns: '/foo/bar/baz/asdf'</span></span><br><span class="line"></span><br><span class="line">path.join(<span class="string">'foo'</span>, &#123;&#125;, <span class="string">'bar'</span>);</span><br><span class="line"><span class="comment">// Throws 'TypeError: Path must be a string. Received &#123;&#125;'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">path.normalize(path)</span><br><span class="line"></span><br><span class="line">path.normalize(<span class="string">'/foo/bar//baz/asdf/quux/..'</span>);</span><br><span class="line"><span class="comment">// Returns: '/foo/bar/baz/asdf'</span></span><br><span class="line">path.normalize(<span class="string">'C:\\temp\\\\foo\\bar\\..\\'</span>);</span><br><span class="line"><span class="comment">// Returns: 'C:\\temp\\foo\\'</span></span><br><span class="line">path.win32.normalize(<span class="string">'C:////temp\\\\/\\/\\/foo/bar'</span>);</span><br><span class="line"><span class="comment">// Returns: 'C:\\temp\\foo\\bar'</span></span><br><span class="line"></span><br><span class="line">path.relative(<span class="keyword">from</span>, to)</span><br><span class="line">path.relative(<span class="string">'/data/orandea/test/aaa'</span>, <span class="string">'/data/orandea/impl/bbb'</span>);</span><br><span class="line"><span class="comment">// Returns: '../../impl/bbb'</span></span><br><span class="line"></span><br><span class="line">path.relative(<span class="string">'C:\\orandea\\test\\aaa'</span>, <span class="string">'C:\\orandea\\impl\\bbb'</span>);</span><br><span class="line"><span class="comment">// Returns: '..\\..\\impl\\bbb'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">path.resolve([...paths])</span><br><span class="line"></span><br><span class="line">path.resolve(<span class="string">'/foo/bar'</span>, <span class="string">'./baz'</span>);</span><br><span class="line"><span class="comment">// Returns: '/foo/bar/baz'</span></span><br><span class="line"></span><br><span class="line">path.resolve(<span class="string">'/foo/bar'</span>, <span class="string">'/tmp/file/'</span>);</span><br><span class="line"><span class="comment">// Returns: '/tmp/file'</span></span><br><span class="line"></span><br><span class="line">path.resolve(<span class="string">'wwwroot'</span>, <span class="string">'static_files/png/'</span>, <span class="string">'../gif/image.gif'</span>);</span><br><span class="line"><span class="comment">// If the current working directory is /home/myself/node,</span></span><br><span class="line"><span class="comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">两个特殊属性</span><br><span class="line">cosole.log(path.delimiter);<span class="comment">// 表示路径分隔符（window是 \ Linux 是 /）</span></span><br><span class="line"><span class="built_in">console</span>.log(path.sep);</span><br><span class="line"><span class="comment">// 环境变量分隔符（Windows中使用；linux中使用；）</span></span><br></pre></td></tr></table></figure>

<p>异步I/O input output输入输出 </p>
<ol>
<li><p>文件操作 文件读写操作</p>
</li>
<li><p>网络操作 网络的请求响应处理</p>
<p>异步处理</p>
</li>
</ol>
<p>在浏览器中也存在异步操作</p>
<ol>
<li><p>定时任务</p>
</li>
<li><p>事件处理</p>
</li>
<li><p>Ajax回调处理</p>
</li>
</ol>
<p>js 的运行时是单线程</p>
<p>引入事件队列机制</p>
<p>Node.js中的事件模型与浏览器中的事件模型类似</p>
<p>单线程 + 事件队列</p>
<p>Node.js中异步执行的任务</p>
<ol>
<li>文件I/O</li>
<li>网络I/O</li>
</ol>
<p>基于回调函数的</p>
<p><a href="http://nodejs.cn/api/" target="_blank" rel="noopener">API 文档 | Node.js 中文网</a></p>
<h3 id="文件操作-File-System-fs"><a href="#文件操作-File-System-fs" class="headerlink" title="文件操作 File System fs"></a>文件操作 File System fs</h3><ul>
<li>文件信息获取</li>
<li>读文件操作</li>
<li>写文件操作</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">fs.writeFile(file, data[, options], callback)</span><br><span class="line"></span><br><span class="line">fs.writeFileSync(file, data[, options])</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> data = <span class="keyword">new</span> <span class="built_in">Uint8Array</span>(Buffer.from(<span class="string">'Hello Node.js'</span>));</span><br><span class="line">fs.writeFile(<span class="string">'message.txt'</span>, data, (err) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'The file has been saved!'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 异步</span></span><br><span class="line"><span class="keyword">let</span> strpath = path.join(__dirname,<span class="string">'data.txt'</span>);</span><br><span class="line">fs.writeFile(strpath,<span class="string">'helloasdfasdf'</span>,’utf8‘,(err)=&gt;&#123;</span><br><span class="line">    <span class="keyword">if</span>(!err)&#123;</span><br><span class="line">	    <span class="built_in">console</span>.log(<span class="string">'写入成功'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> buf = Buffer.from(<span class="string">'hi'</span>);</span><br><span class="line">fs.writeFile(strpath,buf,<span class="string">'utf8'</span>,(err)=&gt;&#123;</span><br><span class="line">   <span class="keyword">if</span>(!err)&#123;</span><br><span class="line">       <span class="built_in">console</span>.log(<span class="string">'文件写入成功'</span>);</span><br><span class="line">   &#125; </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">fs.writeFileSync(strpath, <span class="string">'asdfasdfsadfasdf'</span>,<span class="string">'utf8'</span>);</span><br><span class="line"><span class="comment">// utf-8默认可以不写</span></span><br></pre></td></tr></table></figure>

<p>fs</p>
<ul>
<li>目录操作一（处理内容量比较少的文件） 全部加载到内存</li>
</ul>
<p>创建</p>
<p>fs.mkdir(path[,mode],callback)</p>
<p>fs.mkdirSync(path[,mode])</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const path &#x3D; require(&#39;path&#39;)</span><br><span class="line">const fs &#x3D; require(&#39;fs&#39;)</span><br><span class="line">fs.mkdir(path.join(_dirname,&#39;abc&#39;),(err)&#x3D;&gt;&#123;</span><br><span class="line">	console.log(err);</span><br><span class="line">&#125;)；</span><br><span class="line">fs.mkdirSync(path.join(_dirname,&#39;abc&#39;));</span><br></pre></td></tr></table></figure>

<p>删除目录</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">fs.rmdir(path,callback)</span><br><span class="line">fs.rmdirSync(path)</span><br><span class="line">fs.rmdir(path.join(__dirname,<span class="string">'asdf'</span>),(err)=&gt;&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//同步 </span></span><br><span class="line">fs.rmdirSync(path.join(__dirname,<span class="string">'asdf'</span>));</span><br></pre></td></tr></table></figure>



<p>读取目录</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">fs.readdir(__dirname,(err,files)=&gt;&#123;</span><br><span class="line">	files.forEach(<span class="function">(<span class="params">item,inde</span>)=&gt;</span>&#123;</span><br><span class="line">	 fs.stat(path.join(__dirname,item),(err,stat)=&gt;&#123;</span><br><span class="line">	 	<span class="comment">// 判断是不是目录还是文件</span></span><br><span class="line">         <span class="keyword">if</span>(stat.isFile())&#123;</span><br><span class="line">	 		<span class="built_in">console</span>.log(item,<span class="string">'文件'</span>)</span><br><span class="line">	 	&#125;<span class="keyword">else</span> <span class="keyword">if</span> (stat.isDirectory())&#123;</span><br><span class="line">	 		<span class="built_in">console</span>.log(item,<span class="string">'目录'</span>)</span><br><span class="line">	 	&#125;)</span><br><span class="line">	 &#125;);</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> files = fs.readdirSync(__dirname);</span><br><span class="line">files.forEach(<span class="function">(<span class="params">item,inde</span>)=&gt;</span>&#123;</span><br><span class="line">    fs.stat(path.join(__dirname,item),(err,stat)=&gt;&#123;</span><br><span class="line">        <span class="comment">// 判断是不是目录还是文件</span></span><br><span class="line">        <span class="keyword">if</span>(stat.isFile())&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(item,<span class="string">'文件'</span>)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (stat.isDirectory())&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(item,<span class="string">'目录'</span>)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<p>目录操作2 内容量多 大文件操作 (流式操作)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">fs.createReadStream(path[,options])</span><br><span class="line">fs.createWriteStream(path[,options])</span><br><span class="line"> </span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">let</span> spath = path.join(__dirname,<span class="string">'../03-source'</span>,<span class="string">'file.zip'</span>);</span><br><span class="line"><span class="keyword">let</span> dpath = path.join(<span class="string">'文件\\路\\径'</span>,<span class="string">'文件名称 file.zip'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> readStream = fs.cteateReadStream(spath);</span><br><span class="line"><span class="keyword">let</span> writeStream = fs.createWriteStream(dpath);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 基于事件的处理方式 data 数据读取一部分 end 完成</span></span><br><span class="line">方法一</span><br><span class="line"><span class="keyword">let</span> num = <span class="number">1</span>;</span><br><span class="line">readStream.on(<span class="string">'data'</span>,(chunk)=&gt;&#123;</span><br><span class="line">    num++;</span><br><span class="line">    writeStream.write(chunk);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">readStream.on(<span class="string">'end'</span>,()=&gt;&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'文件处理完成'</span>+num)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// pipe的作用直接把输入流和输出流 </span></span><br><span class="line">方法二</span><br><span class="line">readStream.pipe(writeStream);</span><br><span class="line"></span><br><span class="line">方法三</span><br><span class="line">fs.cteateReadStream(spath).pipe(fs.createWriteStream(dpath))</span><br></pre></td></tr></table></figure>





<p>异步 和 同步</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">异步</span><br><span class="line">fs.stat(path,callback);</span><br><span class="line">console.log('1')</span><br><span class="line">文件操作</span><br><span class="line">cons fs = require('fs');</span><br><span class="line">fs.stat('./data.ext',(err,stat)=&gt;&#123;</span><br><span class="line">	// 一般回调函数的第一个参数是错误对象，如果err为null,表示没有错误，否则表示报错了</span><br><span class="line">	if(errr) return;</span><br><span class="line">	//console.log(err);</span><br><span class="line">	if(stat.isFile())&#123;</span><br><span class="line">		console.log('文件');</span><br><span class="line">	&#125;else if(stat.isDirectory)&#123;</span><br><span class="line">		console,log('目录');</span><br><span class="line">	&#125;</span><br><span class="line">	console.log(stat);</span><br><span class="line">console.log('2')</span><br><span class="line">&#125;); //加入到事件队列</span><br><span class="line">console.log('3')</span><br><span class="line">1 -&gt; 3  -&gt; 2</span><br><span class="line">stat</span><br><span class="line">方法</span><br><span class="line"></span><br><span class="line">属性</span><br><span class="line">atime 文件访问时间</span><br><span class="line">ctime 文件的状态信息反生变化的时间(比如文件的权限)</span><br><span class="line">mtime 文件数据发生改变的时间</span><br><span class="line">birthtime 文件创建的时间</span><br><span class="line">同步操作</span><br><span class="line">console.log('1')</span><br><span class="line">fs.statSync(path)</span><br><span class="line">let ret = fs.statSync('./data.txt');</span><br><span class="line">console.log(ret);</span><br><span class="line">console.log('2')</span><br><span class="line">1 , 2</span><br></pre></td></tr></table></figure>



<h3 id="文件操作案例"><a href="#文件操作案例" class="headerlink" title="文件操作案例"></a>文件操作案例</h3><p>初始化目录结构</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>)</span><br><span class="line"><span class="comment">//目录位置</span></span><br><span class="line"><span class="keyword">let</span> root = <span class="string">'C:\\User\\www'</span>;</span><br><span class="line"><span class="keyword">let</span> fileContent = <span class="string">`</span></span><br><span class="line"><span class="string">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html lang="en"&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">    &lt;meta charset="UTF-8"&gt;</span></span><br><span class="line"><span class="string">    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</span></span><br><span class="line"><span class="string">    &lt;title&gt;Document&lt;/title&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">`</span></span><br><span class="line">初始化数据</span><br><span class="line"><span class="keyword">let</span> initDate = &#123;</span><br><span class="line">    	projectName :<span class="string">'mydemo'</span>,</span><br><span class="line">    	data:[&#123;</span><br><span class="line">            name:<span class="string">'img'</span>,</span><br><span class="line">            type:<span class="string">'dir'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            name:<span class="string">'css'</span>,</span><br><span class="line">            type:<span class="string">'dir'</span></span><br><span class="line">        &#125;,</span><br><span class="line">              &#123;</span><br><span class="line">            name:<span class="string">'js'</span>,</span><br><span class="line">            type:<span class="string">'dir'</span></span><br><span class="line">        &#125;,</span><br><span class="line">         &#123;</span><br><span class="line">            name:<span class="string">'index.html'</span>,</span><br><span class="line">            type:<span class="string">'file'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        ]</span><br><span class="line">&#125;</span><br><span class="line">创建目录跟路径</span><br><span class="line">fs.mkdir(path.join(root,initData.projectName),(err)=&gt;&#123;</span><br><span class="line">    <span class="keyword">if</span>(err) <span class="keyword">return</span>;</span><br><span class="line">    <span class="comment">// 创建子目录和文件</span></span><br><span class="line">    initData.data.forEach(<span class="function">(<span class="params">item</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(item.type  == <span class="string">'dir'</span>)&#123;</span><br><span class="line">            <span class="comment">//创建子目录</span></span><br><span class="line">		  	fs.mkdirSync(path.join(root,initData.projectName,item.name)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(item.type == <span class="string">'file'</span>)&#123;</span><br><span class="line">             <span class="comment">//创建模板文件</span></span><br><span class="line">      		fs.writeFileSync(path.join(root,initData.projectName,item.name),fileContent);</span><br><span class="line"> 		 &#125;</span><br><span class="line"> 	&#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<p>文件信息获取</p>
<p>读文件操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>); <span class="comment">//path</span></span><br><span class="line"><span class="comment">//fs.readFile(file[,options],callback)</span></span><br><span class="line"><span class="keyword">let</span> strpath = path.join(_dirname,<span class="string">'data.txt'</span>)</span><br><span class="line">fs.readFile(strpath,(err,data)=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span>(err) <span class="keyword">return</span>;</span><br><span class="line">	<span class="built_in">console</span>.log(data.toString()); <span class="comment">//Buffer</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//第二种方法 </span></span><br><span class="line"><span class="comment">// 如果有第二个参数并且是编码，那么回调函数函数获取到的数据就是字符串</span></span><br><span class="line"><span class="comment">//如果没有第二个参数，那么得到的就是Buffer实例对象</span></span><br><span class="line"><span class="comment">// 异步 回调函数</span></span><br><span class="line">fs.readFile(strpath,‘utf8’，(err,data)=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span>(err) <span class="keyword">return</span>;</span><br><span class="line">	<span class="built_in">console</span>.log(data; <span class="comment">//Buffer</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 同步操作 返回值</span></span><br><span class="line"><span class="keyword">let</span> ret = fs.readFileSync(strpath,<span class="string">'utf8'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(ret);</span><br></pre></td></tr></table></figure>



<p>写文件操作</p>
<h2 id="包"><a href="#包" class="headerlink" title="包"></a>包</h2><blockquote>
<p>多个模块可以形成包，不过要满足特定的规则才能形成规范的包</p>
</blockquote>
<h3 id="NPM-（node-js-package-management-工具-）"><a href="#NPM-（node-js-package-management-工具-）" class="headerlink" title="NPM （node.js package management(工具)）"></a>NPM （node.js package management(工具)）</h3><p>node.js / node_modules/npm</p>
<p><a href="https://www.npmjs.com/" target="_blank" rel="noopener">npm | build amazing things</a></p>
<blockquote>
<p>全球最大的模块生态系统，里面所有的模块都是开源免费的；也是Node.js的包管理工具。</p>
</blockquote>
<ul>
<li><a href="https://www.npmjs.com/" target="_blank" rel="noopener">官方网站</a></li>
</ul>
<p>`npm(安装node.js包管理工具) nvm (安装node版本)nrm（切换不同node包管理工具的源） cnpm（淘宝的npm源）yarn与npm的功能差不多 ``</p>
<h3 id="npm包安装方式"><a href="#npm包安装方式" class="headerlink" title="npm包安装方式"></a>npm包安装方式</h3><ul>
<li>本地安装  一般用于开发某个具体的功能</li>
</ul>
<p>本地项目下</p>
<p>npm init</p>
<p>npm init -y</p>
<p>package.json文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;name&quot;: &quot;node.package&quot;,</span><br><span class="line">  &quot;version&quot;: &quot;1.0.0&quot;,</span><br><span class="line">  &quot;description&quot;: &quot;&quot;,</span><br><span class="line">  &quot;main&quot;: &quot;index.js&quot;,</span><br><span class="line">  &quot;scripts&quot;: &#123;</span><br><span class="line">    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span><br><span class="line">    &#x2F;&#x2F; &quot;test&quot;:&quot;node index.js&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;author&quot;: &quot;&quot;,</span><br><span class="line">  &quot;license&quot;: &quot;ISC&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">node .</span><br><span class="line">npm run test</span><br></pre></td></tr></table></figure>

<p>安装第三方包 art-template</p>
<ul>
<li>全局安装 -g 安装目录为node.js环境目录下的 / node_modules/npm</li>
<li>全局安装的包用于命令行工具</li>
</ul>
<h3 id="解决npm安装包被墙的问题"><a href="#解决npm安装包被墙的问题" class="headerlink" title="解决npm安装包被墙的问题"></a>解决npm安装包被墙的问题</h3><ul>
<li><p>–registry</p>
<ul>
<li>npm config set registry=https//registry.npm.taobao.org </li>
</ul>
</li>
<li><p>cnpm</p>
<ul>
<li><p>淘宝NPM镜像,与官方NPM的同步频率目前为10分钟一次 </p>
</li>
<li><p>官网: <a href="http://npm.taobao.org/" target="_blank" rel="noopener">http://npm.taobao.org/</a> </p>
</li>
<li><p>npm install -g cnpm –registry=https//registry.npm.taobao.org </p>
</li>
<li><p>使用cnpm安装包: cnpm install 包名</p>
</li>
<li><pre><code>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">- nrm</span><br><span class="line"></span><br><span class="line">  + 作用：修改镜像源 </span><br><span class="line"></span><br><span class="line">  + 项目地址：https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;nrm </span><br><span class="line"></span><br><span class="line">  + 安装：npm install -g nrm</span><br><span class="line"></span><br><span class="line">  +</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">### npm常用命令</span><br><span class="line"></span><br><span class="line">- 安装包 默认安装最新的版本</span><br><span class="line"></span><br><span class="line">- npm install  -g 包名称  (全局安装)</span><br><span class="line"></span><br><span class="line">-  npm install  包名称 （本地安装)</span><br><span class="line"></span><br><span class="line">  npm install -g es-checker</span><br><span class="line"></span><br><span class="line">  es-checker</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F;检测当前node环境支不支持es6 </span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">  安装包的时候可以指定版本 </span><br><span class="line"></span><br><span class="line">  npm install -g 包名称@版本号</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">​	npm install -g i5ting_toc</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;把markdown 文件装换有一个markdown目录树的页面</span><br><span class="line"></span><br><span class="line">使用</span><br><span class="line"></span><br><span class="line">i5ting_toc -f  文件名.md -o</span><br><span class="line"></span><br><span class="line">- 更新包</span><br><span class="line">- npm update -g es-checker</span><br><span class="line">- 卸载包</span><br><span class="line">- npm uninstall -g es-checker</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">packgage.json 文件中添加两个键</span><br><span class="line"></span><br><span class="line">dependencies&#x2F;&#x2F; devDependencies 管理依赖</span><br><span class="line"></span><br><span class="line">--save 向生产环境添加依赖 dependencies </span><br><span class="line"></span><br><span class="line">生产环境（项目部署上线之后的服务器环境）</span><br></pre></td></tr></table></figure>
dependencies 
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">--save-dev 向开发环境添加依赖</span><br><span class="line"></span><br><span class="line">开发环境（平时开发使用的环境）DevDependencies 开发环境使用</span><br></pre></td></tr></table></figure>
devDependencies
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
npm install 安装所有环境下的包 

</code></pre></li>
</ul>
</li>
</ul>
<p>npm install –production<br>// 只会安装dependencies中的包不会安装devDependencies中的包</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">### yarn基本使用 facebook开发的</span><br><span class="line"></span><br><span class="line">- 类比npm基本使用</span><br><span class="line"></span><br><span class="line">npm的缺点 ： </span><br><span class="line"></span><br><span class="line">有些包没有办法更新</span><br><span class="line"></span><br><span class="line">npm性能不太好</span><br><span class="line"></span><br><span class="line">安装</span><br><span class="line"></span><br><span class="line">npm install -g yarn</span><br><span class="line"></span><br><span class="line">初始化</span><br><span class="line"></span><br><span class="line">npm init</span><br><span class="line"></span><br><span class="line">yarn init</span><br><span class="line"></span><br><span class="line">安装包</span><br><span class="line"></span><br><span class="line">npm install xxx –save</span><br><span class="line"></span><br><span class="line">yarn add xxx</span><br><span class="line"></span><br><span class="line">移除包</span><br><span class="line"></span><br><span class="line">npm uninstall xxx</span><br><span class="line"></span><br><span class="line">yarn remove xxx</span><br><span class="line"></span><br><span class="line">更新包</span><br><span class="line"></span><br><span class="line">npm update xxx</span><br><span class="line"></span><br><span class="line">yarn upgrade xxx</span><br><span class="line"></span><br><span class="line">安装开发依赖的包</span><br><span class="line"></span><br><span class="line">npm install xxx   --save-dev</span><br><span class="line"></span><br><span class="line">yarn add xxx --dev</span><br><span class="line"></span><br><span class="line">全局安装</span><br><span class="line"></span><br><span class="line">npm install -g xxx</span><br><span class="line"></span><br><span class="line">yarn global add xxx</span><br><span class="line"></span><br><span class="line">设置包的镜像地址</span><br><span class="line"></span><br><span class="line">npm config set registry url</span><br><span class="line"></span><br><span class="line">yarn config set registry url</span><br><span class="line"></span><br><span class="line">安装所有依赖</span><br><span class="line"></span><br><span class="line">npm install </span><br><span class="line"></span><br><span class="line">yarn install</span><br><span class="line"></span><br><span class="line">执行包</span><br><span class="line"></span><br><span class="line">npm run </span><br><span class="line"></span><br><span class="line">yarn run</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## 自定义包</span><br><span class="line"></span><br><span class="line">### 包的规范</span><br><span class="line"></span><br><span class="line">- package.json必须在包的顶层目录下</span><br><span class="line">- 二进制文件应该在bin目录下</span><br><span class="line">- JavaScript代码应该在lib目录下</span><br><span class="line">- 文档应该在doc目录下</span><br><span class="line">- 单元测试应该在test目录下</span><br><span class="line"></span><br><span class="line">### package.json</span><br></pre></td></tr></table></figure>
<p>入口文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#96;&#96;&#96;json</span><br><span class="line">&#123;</span><br><span class="line">  &quot;name&quot;: &quot;node.package&quot;,</span><br><span class="line">  &quot;version&quot;: &quot;1.0.0&quot;, &#x2F;&#x2F;语义化版本</span><br><span class="line">  &quot;description&quot;: &quot;&quot;,</span><br><span class="line">  &quot;main&quot;: &quot;index.js&quot;,&#x2F;&#x2F; 入口文件</span><br><span class="line">  &quot;scripts&quot;: &#123;</span><br><span class="line">    &quot;test&quot;: &quot;node index.js&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;author&quot;: &quot;&quot;,</span><br><span class="line">  &quot;license&quot;: &quot;ISC&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">markdown =&gt; html</span><br><span class="line">npm install markdown-it --save</span><br><span class="line">npm markdowk-it包</span><br><span class="line">https:<span class="comment">//www.npmjs.com/package/markdown-it</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// node.js, "classic" way:</span></span><br><span class="line"><span class="keyword">var</span> MarkdownIt = <span class="built_in">require</span>(<span class="string">'markdown-it'</span>),</span><br><span class="line">    md = <span class="keyword">new</span> MarkdownIt();</span><br><span class="line"><span class="keyword">var</span> result = md.render(<span class="string">'# markdown-it rulezz!'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// node.js, the same, but with sugar:</span></span><br><span class="line"><span class="keyword">var</span> md = <span class="built_in">require</span>(<span class="string">'markdown-it'</span>)();</span><br><span class="line"><span class="keyword">var</span> result = md.render(<span class="string">'# markdown-it rulezz!'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// browser without AMD, added to "window" on script load</span></span><br><span class="line"><span class="comment">// Note, there is no dash in "markdownit".</span></span><br><span class="line"><span class="keyword">var</span> md = <span class="built_in">window</span>.markdownit();</span><br><span class="line"><span class="keyword">var</span> result = md.render(<span class="string">'# markdown-it rulezz!'</span>);</span><br></pre></td></tr></table></figure>



<h3 id="package-json字段分析"><a href="#package-json字段分析" class="headerlink" title="package.json字段分析"></a>package.json字段分析</h3><ul>
<li>name：包的名称，必须是唯一的，由小写英文字母、数字和下划线组成，不能包含空格</li>
<li>description：包的简要说明</li>
<li>version：符合语义化版本识别规范的版本字符串</li>
<li>keywords：关键字数组，通常用于搜索</li>
<li>maintainers：维护者数组，每个元素要包含name、email（可选）、web（可选）字段</li>
<li>contributors：贡献者数组，格式与maintainers相同。包的作者应该是贡献者数组的第一- 个元素</li>
<li>bugs：提交bug的地址，可以是网站或者电子邮件地址</li>
<li>licenses：许可证数组，每个元素要包含type（许可证名称）和url（链接到许可证文本的- 地址）字段</li>
<li>repositories：仓库托管地址数组，每个元素要包含type（仓库类型，如git）、url（仓- 库的地址）和path（相对于仓库的路径，可选）字段</li>
<li>dependencies：<code>生产环境包的依赖</code>，一个关联数组，由包的名称和版本号组成</li>
<li>devDependencies：<code>开发环境包的依赖</code>，一个关联数组，由包的名称和版本号组成</li>
</ul>
<h3 id="自定义包案例"><a href="#自定义包案例" class="headerlink" title="自定义包案例"></a>自定义包案例</h3><p>动态网站开发</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>)</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line"><span class="keyword">const</span> querystring = <span class="built_in">require</span>(<span class="string">'querystring'</span>)</span><br><span class="line"><span class="keyword">const</span> scoreData = <span class="built_in">require</span>(./scores.json)</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req,res</span>)=&gt;</span>&#123;</span><br><span class="line">	<span class="comment">// 路由（请求路径 + 请求方式）</span></span><br><span class="line">	<span class="comment">// 查询成绩的入口地址 query</span></span><br><span class="line">	<span class="keyword">if</span>(req.url.statsWith(<span class="string">'/query'</span>) &amp;&amp; req.method == <span class="string">'GET'</span>)&#123;</span><br><span class="line">		fs.readFile(path.join(__dirname,<span class="string">'view'</span>,<span class="string">'index.tpl'</span>),<span class="string">'utf-8'</span>,(err,content)=&gt;&#123;</span><br><span class="line">			<span class="keyword">if</span>(err)&#123;</span><br><span class="line">			res.writeHead(<span class="number">500</span>,&#123;</span><br><span class="line">				<span class="string">'Content-Type'</span>:<span class="string">'text/plain;charset=utf-8'</span></span><br><span class="line">			&#125;);</span><br><span class="line">			res.end(<span class="string">'服务器错误，请与管理员联系'</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">            res.end(content);        </span><br><span class="line">    &#125;); </span><br><span class="line">	&#125;<span class="keyword">else</span> <span class="keyword">if</span>(req.url.startsWith(<span class="string">'/score'</span>) &amp;&amp; req.method == <span class="string">'POST'</span>)&#123;</span><br><span class="line">	<span class="comment">// 获取成绩的结果 score</span></span><br><span class="line">		<span class="keyword">let</span> pdata = <span class="string">''</span>;</span><br><span class="line">		req.on(<span class="string">'data'</span>,(chunk)=&gt;&#123;</span><br><span class="line">			pdata += chunk;</span><br><span class="line">		&#125;);</span><br><span class="line">		req.on(<span class="string">'end'</span>,()=&gt;&#123;</span><br><span class="line">			<span class="keyword">let</span> obj = querystring.parse(pdata);</span><br><span class="line">			<span class="keyword">let</span> result = scoreData[obj.code];</span><br><span class="line">            fs.readFile(path.join(__dirname,<span class="string">"view"</span>,<span class="string">"result.tpl"</span>),<span class="string">"utf-8"</span>,(err,content)=&gt;&#123;</span><br><span class="line">               	<span class="keyword">if</span>(err)&#123;</span><br><span class="line">			res.writeHead(<span class="number">500</span>,&#123;</span><br><span class="line">				<span class="string">'Content-Type'</span>:<span class="string">'text/plain;charset=utf-8'</span></span><br><span class="line">			&#125;);</span><br><span class="line">			res.end(<span class="string">'服务器错误，请与管理员联系'</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">             <span class="comment">// 返回内容之前要进行数据渲染</span></span><br><span class="line">             content = content.replace(<span class="string">'$$chinese$$'</span>,result.chinese);   </span><br><span class="line">             content = content.replace(<span class="string">'$$math$$'</span>,result.math);    </span><br><span class="line">             content = content.replace(<span class="string">'$$english$$'</span>,result.english);    </span><br><span class="line">             content = content.replace(<span class="string">'$$summary$$'</span>,result.summary);    </span><br><span class="line">       		res.end(content);</span><br><span class="line">            &#125;);</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;).listen(<span class="number">3000</span>,()=&gt;&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">'running.....'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">scores.json</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">	&quot;no123&quot;:&#123;</span><br><span class="line">		&quot;chinese&quot;:&quot;100&quot;,</span><br><span class="line">		&quot;math&quot;:&quot;140&quot;,</span><br><span class="line">		&quot;english&quot;:&quot;149&quot;,</span><br><span class="line">		&quot;summary&quot;:&quot;289&quot;</span><br><span class="line">	&#125;,</span><br><span class="line">	&quot;no124&quot;:&#123;</span><br><span class="line">		&quot;chinese&quot;:&quot;100&quot;,</span><br><span class="line">		&quot;math&quot;:&quot;140&quot;,</span><br><span class="line">		&quot;english&quot;:&quot;149&quot;,</span><br><span class="line">		&quot;summary&quot;:&quot;289&quot;</span><br><span class="line">	&#125;,</span><br><span class="line">	&quot;no125&quot;:&#123;</span><br><span class="line">		&quot;chinese&quot;:&quot;100&quot;,</span><br><span class="line">		&quot;math&quot;:&quot;140&quot;,</span><br><span class="line">		&quot;english&quot;:&quot;149&quot;,</span><br><span class="line">		&quot;summary&quot;:&quot;289&quot;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">view/index.tpl // 模板</span><br><span class="line">查询页面</span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">"http://localhost:3000/score/"</span> <span class="attr">method</span>=<span class="string">"post"</span>&gt;</span></span><br><span class="line">        请输入考号:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"code"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"查询"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br><span class="line">// 结果页面 result.tpl</span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>语文:$$chinese$$<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>数学:$$math$$<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>外语:$$english$$<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span>综合:$$summary$$<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>Web开发-初步实现服务</p>
<p>请求路径分发</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//初步实现服务器功能</span></span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="comment">//创建服务器实例对象</span></span><br><span class="line"><span class="keyword">let</span> server = http.createServer();</span><br><span class="line"><span class="comment">//绑定请求事件</span></span><br><span class="line">server.on(<span class="string">'request'</span>,(req,res)=&gt;&#123;</span><br><span class="line">    res.end(<span class="string">'hello'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//监听端口</span></span><br><span class="line">server.listen(<span class="number">3000</span>);</span><br><span class="line"><span class="comment">// 127.0.0.1:3000 // localhost:3000</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//简单的方法</span></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req.res</span>)=&gt;</span>&#123;</span><br><span class="line">    res.end(<span class="string">'hello'</span>);</span><br><span class="line">&#125;).listen(<span class="number">3000</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 指定IP地址</span></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req.res</span>)=&gt;</span>&#123;</span><br><span class="line">    res.end(<span class="string">'hello'</span>);</span><br><span class="line">&#125;).listen(<span class="number">3000</span>,<span class="string">'本地IP地址192.168.3.4'</span>,()=&gt;&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'running...'</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>初步实现静态资源</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">处理请求路径的分发</span><br><span class="line"><span class="number">1.</span>req 对象是Class: http.IncomingMessage的实例对象</span><br><span class="line"><span class="number">2.</span>res对象是Class : http.ServerResponse的实例对象</span><br><span class="line">response.end([data[, encoding]][, callback])</span><br><span class="line"><span class="comment">// end方法用来完成响应，只能执行一次</span></span><br><span class="line">response.write(chunk[, encoding][, callback]) </span><br><span class="line"><span class="comment">// write向客户端响应内容，可以写多次</span></span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">     <span class="comment">// res.end(req.url); url的地址 req.url可以获取URL中的路径(端口之后部分)</span></span><br><span class="line">    <span class="comment">// res.end(req.url);</span></span><br><span class="line">    <span class="keyword">if</span> (req.url.startsWith(<span class="string">'/index'</span>)) &#123;</span><br><span class="line">        res.end(<span class="string">'index'</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (req.url.startsWith(<span class="string">'/about'</span>)) &#123;</span><br><span class="line">        res.write(<span class="string">'hello'</span>)</span><br><span class="line">        res.write(<span class="string">'new'</span>)</span><br><span class="line">        res.write(<span class="string">'world'</span>)</span><br><span class="line">        res.end();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        res.end(<span class="string">'404'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(<span class="number">3300</span>, <span class="string">'127.0.0.1'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'running'</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>文件的读取操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">根据路径读取文件的内容，并且响应到浏览器端</span><br><span class="line"><span class="comment">// 响应完整的页面信息</span></span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> readFile = <span class="function">(<span class="params">url, res</span>) =&gt;</span> &#123;</span><br><span class="line">    fs.readFile(path.join(__dirname, <span class="string">'www'</span>, url), <span class="string">'utf8'</span>, (err, fileContent) =&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            res.end(<span class="string">'server error'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            res.end(fileContent);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//处理路径的分发</span></span><br><span class="line">    <span class="keyword">if</span> (req.url.startsWith(<span class="string">'/index'</span>)) &#123;</span><br><span class="line">        readFile(<span class="string">'index.html'</span>, res);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (req.url.startsWith(<span class="string">'/about'</span>)) &#123;</span><br><span class="line">        readFile(<span class="string">'about.html'</span>, res);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// writeHead 数组相应类型和编码</span></span><br><span class="line">        res.writeHead(<span class="number">200</span>, &#123;</span><br><span class="line">            <span class="string">'Content-Type'</span>: <span class="string">'text/plain;charset= utf8'</span></span><br><span class="line">        &#125;);</span><br><span class="line">        res.end(<span class="string">'404网页'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(<span class="number">3000</span>, <span class="string">'127.0.0.1'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'running...'</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">封装</span><br><span class="line"><span class="number">06.</span>js</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"><span class="keyword">const</span> mime = <span class="built_in">require</span>(<span class="string">'./mine.json'</span>)</span><br><span class="line"><span class="comment">// 模块</span></span><br><span class="line">exports.staticServer = <span class="function">(<span class="params">req, res, root</span>) =&gt;</span> &#123;</span><br><span class="line">    fs.readFile(path.join(__dirname, <span class="string">"www"</span>, req.url), (err, fileContent) =&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            res.writeHead(<span class="number">404</span>, &#123;</span><br><span class="line">                <span class="string">"Content-Type"</span>: <span class="string">"text/plain;charset=utf8"</span>,</span><br><span class="line">            &#125;);</span><br><span class="line">            res.end(<span class="string">"404页面"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 获取请求文件的后缀</span></span><br><span class="line">            <span class="keyword">let</span> dtype = <span class="string">'text/html'</span>;</span><br><span class="line">            <span class="comment">// 如果请求的文件后缀合理，就获取标准的响应格式</span></span><br><span class="line">            <span class="keyword">let</span> ext = path.extname(req.url);</span><br><span class="line">            <span class="keyword">if</span> (mime[ext]) &#123;</span><br><span class="line">                dtype = mime[ext];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 如果响应的内容是文本，就设置成utf8</span></span><br><span class="line">            <span class="keyword">if</span> (dtype.startsWith(<span class="string">'text'</span>)) &#123;</span><br><span class="line">                dtype += <span class="string">';charset=utf8'</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 设置响应头信息</span></span><br><span class="line">            res.writeHead(<span class="number">200</span>, &#123;</span><br><span class="line">                <span class="string">'Content-Type'</span>: dtype</span><br><span class="line">            &#125;);</span><br><span class="line">            res.end(fileContent);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">"http"</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"><span class="keyword">const</span> ss = <span class="built_in">require</span>(<span class="string">'./06.js'</span>)</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">    ss.staticServer(req,res,path.join(_dirname,<span class="string">'www'</span>));</span><br><span class="line">&#125;)</span><br><span class="line">  .listen(<span class="number">3000</span>, <span class="string">"127.0.0.1"</span>, () =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"running..."</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>







<h2 id="Web开发概述"><a href="#Web开发概述" class="headerlink" title="Web开发概述"></a>Web开发概述</h2><ul>
<li>Node.js服务器模型与 php服务器模型的区别</li>
</ul>
<p><img src="https://i.loli.net/2020/05/01/XChWwNZLqicjGER.png" alt=""></p>
<p>静态</p>
<h2 id="Node-js实现静态网站功能"><a href="#Node-js实现静态网站功能" class="headerlink" title="Node.js实现静态网站功能"></a>Node.js实现静态网站功能</h2><ul>
<li>使用http模块初步实现服务器功能</li>
<li>实现静态服务器功能</li>
</ul>
<h2 id="参数传递与获取"><a href="#参数传递与获取" class="headerlink" title="参数传递与获取"></a>参数传递与获取</h2><ul>
<li>get参数获取</li>
<li></li>
<li>post参数获取</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">get</span>参数处理</span><br><span class="line">// parse 字符串=&gt;对象</span><br><span class="line">const url = require('url');</span><br><span class="line">let str = 'https://www.baidu.com/';</span><br><span class="line">let ret = url.parse(str);</span><br><span class="line">console.log(ret)</span><br><span class="line">console.log(ret.query)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Url &#123;</span><br><span class="line">  protocol: <span class="string">'https:'</span>,</span><br><span class="line">  slashes: <span class="literal">true</span>,</span><br><span class="line">  auth: <span class="literal">null</span>,</span><br><span class="line">  host: <span class="string">'www.baidu.com'</span>,</span><br><span class="line">  port: <span class="literal">null</span>,</span><br><span class="line">  hostname: <span class="string">'www.baidu.com'</span>,</span><br><span class="line">  hash: <span class="literal">null</span>,</span><br><span class="line">  search: <span class="literal">null</span>,</span><br><span class="line">  query: <span class="literal">null</span>,</span><br><span class="line">  pathname: <span class="string">'/'</span>,</span><br><span class="line">  path: <span class="string">'/'</span>,</span><br><span class="line">  href: <span class="string">'https://www.baidu.com/'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">'url'</span>);</span><br><span class="line"><span class="keyword">let</span> str = <span class="string">'http://www.baidu.com/abc/qqq?flag=123&amp;keyword=Java'</span>;</span><br><span class="line"><span class="keyword">let</span> ret = url.parse(str,<span class="literal">true</span>);</span><br><span class="line"><span class="built_in">console</span>.log(ret.query.flag);</span><br><span class="line"><span class="built_in">console</span>.log(ret.query.keyword);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// format对象 =&gt; 字符串</span></span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  protocol: <span class="string">'https:'</span>,</span><br><span class="line">  slashes: <span class="literal">true</span>,</span><br><span class="line">  auth: <span class="literal">null</span>,</span><br><span class="line">  host: <span class="string">'www.baidu.com'</span>,</span><br><span class="line">  port: <span class="literal">null</span>,</span><br><span class="line">  hostname: <span class="string">'www.baidu.com'</span>,</span><br><span class="line">  hash: <span class="literal">null</span>,</span><br><span class="line">  search: <span class="literal">null</span>,</span><br><span class="line">  query: <span class="literal">null</span>,</span><br><span class="line">  pathname: <span class="string">'/'</span>,</span><br><span class="line">  path: <span class="string">'/'</span>,</span><br><span class="line">  href: <span class="string">'https://www.baidu.com/'</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> ret1 = url.format(obj);</span><br><span class="line"><span class="built_in">console</span>.log(ret1);</span><br></pre></td></tr></table></figure>



<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//127.0.0.1:3000/?usename=asdfsad&amp;password=123</span></span><br><span class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">'url'</span>);</span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123; </span><br><span class="line">    <span class="keyword">let</span> obj = url.parse(req.url, <span class="literal">true</span>);</span><br><span class="line">    res.end(obj.query.usename + <span class="string">'======='</span> + obj.query.password);</span><br><span class="line">&#125;).listen(<span class="number">3000</span>, () =&gt; &#123; </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'running....'</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>





<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">post参数处理</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> querystring = <span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"><span class="comment">// patse 方法的作用就是把字符串转成对象</span></span><br><span class="line"><span class="keyword">let</span> param = <span class="string">'foo=bar&amp;abc=xyz&amp;abc=123'</span>;</span><br><span class="line"><span class="keyword">let</span> obj = querystring.parse(param);</span><br><span class="line"><span class="built_in">console</span>,log(obj);</span><br><span class="line"><span class="built_in">console</span>,log(obj.usename);</span><br><span class="line"></span><br><span class="line"><span class="comment">// stringify的作用就是把字符串转为对象</span></span><br><span class="line"><span class="keyword">let</span> obj1 = &#123;</span><br><span class="line">    flag:<span class="string">'122'</span>,</span><br><span class="line">    abc:<span class="string">'hello'</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> str1 = querystring.stringify(obj1);</span><br><span class="line"><span class="built_in">console</span>.log(str1);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> querystring = <span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>)</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (req.url.startsWith(<span class="string">'/login'</span>)) &#123;</span><br><span class="line">        <span class="keyword">let</span> pdata = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">        req.on(<span class="string">'data'</span>, (chunk) =&gt; &#123;</span><br><span class="line">            pdata += chunk;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        req.on(<span class="string">'end'</span>, () =&gt; &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(pdata);</span><br><span class="line">            <span class="keyword">let</span> obj = querystring.parse(pdata);</span><br><span class="line">            res.end(obj.username + <span class="string">'-----'</span> + obj.password)</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(<span class="number">3000</span>, () =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'running.....'</span>)</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">username </span><br><span class="line">password</span><br></pre></td></tr></table></figure>





<p>Node.js的web开发相关的内容</p>
<ol>
<li>Node.js不需要依赖第三方应用软件（Apache），可以基于api自已实现</li>
<li>实现静态资源服务器</li>
<li>路由处理</li>
<li>动态网站</li>
<li>模板引擎</li>
<li>get和post参数传参和处理</li>
</ol>
<p>Web开发框架：express</p>
<h2 id="动态网站开发"><a href="#动态网站开发" class="headerlink" title="动态网站开发"></a>动态网站开发</h2><ul>
<li>创建服务器实现动态网站效果</li>
</ul>
<h2 id="模板引擎"><a href="#模板引擎" class="headerlink" title="模板引擎"></a>模板引擎</h2><ul>
<li>理解模板引擎本质</li>
<li>引擎基本使用</li>
</ul>
<h2 id="Express基本使用"><a href="#Express基本使用" class="headerlink" title="Express基本使用"></a>Express基本使用</h2><ul>
<li>静态服务器</li>
<li>路由</li>
<li>中间件</li>
<li>模板引擎整合</li>
<li>常用API基本使用</li>
</ul>
<h2 id="Express基本介绍和使用"><a href="#Express基本介绍和使用" class="headerlink" title="Express基本介绍和使用"></a>Express基本介绍和使用</h2><p><a href="https://www.expressjs.com.cn/" target="_blank" rel="noopener">Express</a><a href="https://www.expressjs.com.cn/changelog/4x.html#4.17.1" target="_blank" rel="noopener">4.17.1</a>基于 <a href="https://nodejs.org/en/" target="_blank" rel="noopener">Node.js</a> 平台，快速、开放、极简的 Web 开发框架</p>
<p><a href="https://www.expressjs.com.cn/" target="_blank" rel="noopener">Express - 基于 Node.js 平台的 web 应用开发框架 - Express 中文文档 | Express 中文网</a></p>
<p>安装</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm init-y</span><br><span class="line">npm install express -save</span><br></pre></td></tr></table></figure>

<p>hello world案例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">app.js</span><br><span class="line"></span><br><span class="line">方法一</span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>)</span><br><span class="line"><span class="keyword">const</span> app = express()</span><br><span class="line"><span class="comment">// 相当于 const app = require('express')();</span></span><br><span class="line"><span class="keyword">const</span> port = <span class="number">3000</span></span><br><span class="line"><span class="comment">//绑定路由</span></span><br><span class="line">app.get(<span class="string">'/'</span>, (req, res) =&gt; res.send(<span class="string">'Hello World!'</span>))</span><br><span class="line"><span class="comment">//监听端口</span></span><br><span class="line">app.listen(port, () =&gt; <span class="built_in">console</span>.log(<span class="string">`Example app listening on port <span class="subst">$&#123;port&#125;</span>!`</span>))</span><br><span class="line"></span><br><span class="line">方法二</span><br><span class="line">app.get(<span class="string">'/'</span>, (req, res) =&gt; &#123; </span><br><span class="line">    res.send(<span class="string">'ok'</span>);</span><br><span class="line">&#125;).listen(port, () =&gt; &#123; </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`Example app listening on port <span class="subst">$&#123;port&#125;</span>!`</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> server = app.get(<span class="string">'/'</span>,(req,res)=&gt;&#123;</span><br><span class="line">     res.send(<span class="string">'ok'</span>);</span><br><span class="line">&#125;);</span><br><span class="line">server.listen(<span class="number">3000</span>,()=&gt;&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">`Example app listening on port <span class="subst">$&#123;port&#125;</span>!`</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">模板引擎整合: art-template</span><br><span class="line">npm install art-template --sace</span><br></pre></td></tr></table></figure>

<p>使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const express &#x3D; require(&#39;express&#39;);</span><br><span class="line">const template &#x3D; require(&#39;art0template&#39;);</span><br><span class="line">const app &#x3D; express();</span><br></pre></td></tr></table></figure>





<h2 id="Express路由处理"><a href="#Express路由处理" class="headerlink" title="Express路由处理"></a>Express路由处理</h2><h2 id="Express中间件"><a href="#Express中间件" class="headerlink" title="Express中间件"></a>Express中间件</h2><h3 id="初始"><a href="#初始" class="headerlink" title="初始"></a>初始</h3><h3 id="中间件"><a href="#中间件" class="headerlink" title="中间件"></a>中间件</h3><h3 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h3><h3 id="模板"><a href="#模板" class="headerlink" title="模板"></a>模板</h3><ul>
<li>中间件</li>
<li>参数处理</li>
<li>模板引擎整合</li>
</ul>
<p><img src="https://i.loli.net/2020/05/01/1ZiocFOTmGEKw4Q.png" alt=""></p>
<h2 id="Express图书管理案例"><a href="#Express图书管理案例" class="headerlink" title="Express图书管理案例"></a>Express图书管理案例</h2><h2 id="Node-js操作数据库"><a href="#Node-js操作数据库" class="headerlink" title="Node.js操作数据库"></a>Node.js操作数据库</h2><ul>
<li>Mysql环境准备</li>
</ul>
<h2 id="mysql第三方包基本使用"><a href="#mysql第三方包基本使用" class="headerlink" title="mysql第三方包基本使用"></a>mysql第三方包基本使用</h2><ul>
<li>操作数据库基本步骤</li>
<li>实现增删改成基本操作</li>
</ul>
<h2 id="基于数据库实现登录功能"><a href="#基于数据库实现登录功能" class="headerlink" title="基于数据库实现登录功能"></a>基于数据库实现登录功能</h2><h2 id="Express整合数据库实现增删改查业务"><a href="#Express整合数据库实现增删改查业务" class="headerlink" title="Express整合数据库实现增删改查业务"></a>Express整合数据库实现增删改查业务</h2><h2 id="后台API开发"><a href="#后台API开发" class="headerlink" title="后台API开发"></a>后台API开发</h2><ul>
<li>基于数据库的json接口开发</li>
<li>基于数据库的jsonp接口开发</li>
</ul>
<h2 id="图书管理系统整合数据库"><a href="#图书管理系统整合数据库" class="headerlink" title="图书管理系统整合数据库"></a>图书管理系统整合数据库</h2><h2 id="后台接口开发"><a href="#后台接口开发" class="headerlink" title="后台接口开发"></a>后台接口开发</h2><ul>
<li>json接口</li>
<li>jsonp接口</li>
<li>restful接口</li>
</ul>
<h2 id="基于Ajax与后台接口的前端渲染案例"><a href="#基于Ajax与后台接口的前端渲染案例" class="headerlink" title="基于Ajax与后台接口的前端渲染案例"></a>基于Ajax与后台接口的前端渲染案例</h2><ul>
<li>前端渲染与后端渲染分析</li>
</ul>
<h2 id="从服务器主动发送请求"><a href="#从服务器主动发送请求" class="headerlink" title="从服务器主动发送请求"></a>从服务器主动发送请求</h2><ul>
<li>基于核心模块主动发送请求</li>
</ul>
<h2 id="调用第三方接口"><a href="#调用第三方接口" class="headerlink" title="调用第三方接口"></a>调用第三方接口</h2><ul>
<li>针对第三方接口进行二次封装</li>
<li>扩展天气查询功能</li>
</ul>
<p>项目</p>
<h2 id="Node-js网络爬虫"><a href="#Node-js网络爬虫" class="headerlink" title="Node.js网络爬虫"></a>Node.js网络爬虫</h2><p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install cheerio</span><br></pre></td></tr></table></figure>

<p>爬取链接</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> http = <span class="built_in">require</span>(<span class="string">"http"</span>);</span><br><span class="line"><span class="keyword">var</span> cheerio = <span class="built_in">require</span>(<span class="string">"cheerio"</span>);</span><br><span class="line"><span class="comment">//准备抓取的网站链接</span></span><br><span class="line"><span class="keyword">var</span> dataUrl = <span class="string">"http://www.mcake.com/shop/110/index.html#mainer_top"</span>;</span><br><span class="line">http.get(dataUrl, <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> str = <span class="string">""</span>;</span><br><span class="line">    <span class="comment">//绑定方法，获取网页数据</span></span><br><span class="line">    res.on(<span class="string">"data"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">chunk</span>) </span>&#123;</span><br><span class="line">        str += chunk;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">//数据获取完毕</span></span><br><span class="line">    res.on(<span class="string">"end"</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//调用下方的函数，得到返回值，即是我们想要的img的src</span></span><br><span class="line">        <span class="keyword">var</span> data = getData(str);</span><br><span class="line">        <span class="built_in">console</span>.log(data);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//根据得到的数据，处理得到自己想要的</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params">str</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//沿用JQuery风格，定义$</span></span><br><span class="line">    <span class="keyword">var</span> $ = cheerio.load(str);</span><br><span class="line">    <span class="comment">//获取的数据数组</span></span><br><span class="line">    <span class="keyword">var</span> arr = $(<span class="string">".pro_box a:nth-child(1) img"</span>);</span><br><span class="line">    <span class="keyword">var</span> dataTemp = [];</span><br><span class="line">    <span class="comment">//遍历得到数据的src，并放入以上定义的数组中</span></span><br><span class="line">    arr.each(<span class="function"><span class="keyword">function</span> (<span class="params">k, v</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> src = $(v).attr(<span class="string">"src"</span>);</span><br><span class="line">        dataTemp.push(src);</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">//返回出去</span></span><br><span class="line">    <span class="keyword">return</span> dataTemp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>今天再看《Node.js经典入门》的时候，按照教材通过npm安装express，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">express -g express</span><br></pre></td></tr></table></figure>

<p>再运行命令行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">express express_example</span><br></pre></td></tr></table></figure>

<p>的时候报错“express不是内部或外部命令”，后来google搜了一下解决方法：<br>原来express默认安装是最新的版本，已经是4.x.x的版本。而最新express4.0+版本中将命令工具分出来了，所以必须要安装express-generator。解决方法是执行命令行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g express-generator</span><br></pre></td></tr></table></figure>

<p>等到install完成后，再 <code>express -v</code>测试安装成功没~<br>一般是没问题的，除非你最开始安装express的时候是安装在本地，而不是“-g”，如果是那样，可以参考</p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="heweiliang 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="heweiliang 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/tags/node/" rel="tag"># node-</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/20/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E9%A1%B9%E7%9B%AE/" rel="prev" title="计算机基础/项目">
      <i class="fa fa-chevron-left"></i> 计算机基础/项目
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/01/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/LInux%E6%9C%8D%E5%8A%A1%E5%99%A8/linux%E5%91%BD%E4%BB%A4/" rel="next" title="计算机基础/LInux服务器/linux命令">
      计算机基础/LInux服务器/linux命令 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC81MDY1MC8yNzEzMw=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Node"><span class="nav-number">1.</span> <span class="nav-text">Node</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#VS插件"><span class="nav-number">1.1.</span> <span class="nav-text">VS插件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Node-Debug"><span class="nav-number">1.1.1.</span> <span class="nav-text">Node Debug</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Search-node-modules"><span class="nav-number">1.1.2.</span> <span class="nav-text">Search node_modules</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Node-Exec"><span class="nav-number">1.1.3.</span> <span class="nav-text">Node Exec</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Node-Snippets"><span class="nav-number">1.1.4.</span> <span class="nav-text">Node Snippets</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基础"><span class="nav-number">1.2.</span> <span class="nav-text">基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#cmd命令"><span class="nav-number">1.2.1.</span> <span class="nav-text">cmd命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#node-js-安装"><span class="nav-number">1.2.2.</span> <span class="nav-text">node.js 安装</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#settings-txt文件的配置"><span class="nav-number">1.2.2.1.</span> <span class="nav-text">settings.txt文件的配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#环境变量的配置"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">环境变量的配置</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nvm常用的命令"><span class="nav-number">1.2.3.</span> <span class="nav-text">nvm常用的命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#node-js版本命名规则"><span class="nav-number">1.2.3.1.</span> <span class="nav-text">node.js版本命名规则</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#概念"><span class="nav-number">1.2.4.</span> <span class="nav-text">概念</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#服务器端模块化"><span class="nav-number">1.3.</span> <span class="nav-text">服务器端模块化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ES6常用语法"><span class="nav-number">1.4.</span> <span class="nav-text">ES6常用语法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Buffer基本操作"><span class="nav-number">1.5.</span> <span class="nav-text">Buffer基本操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#核心模块API-Buffer-path-fs"><span class="nav-number">1.6.</span> <span class="nav-text">核心模块API Buffer &#x2F;&#x2F; path &#x2F;&#x2F; fs</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#路径操作"><span class="nav-number">1.6.1.</span> <span class="nav-text">路径操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件操作-File-System-fs"><span class="nav-number">1.6.2.</span> <span class="nav-text">文件操作 File System fs</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件操作案例"><span class="nav-number">1.6.3.</span> <span class="nav-text">文件操作案例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#包"><span class="nav-number">1.7.</span> <span class="nav-text">包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#NPM-（node-js-package-management-工具-）"><span class="nav-number">1.7.1.</span> <span class="nav-text">NPM （node.js package management(工具)）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#npm包安装方式"><span class="nav-number">1.7.2.</span> <span class="nav-text">npm包安装方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决npm安装包被墙的问题"><span class="nav-number">1.7.3.</span> <span class="nav-text">解决npm安装包被墙的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#package-json字段分析"><span class="nav-number">1.7.4.</span> <span class="nav-text">package.json字段分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义包案例"><span class="nav-number">1.7.5.</span> <span class="nav-text">自定义包案例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Web开发概述"><span class="nav-number">1.8.</span> <span class="nav-text">Web开发概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Node-js实现静态网站功能"><span class="nav-number">1.9.</span> <span class="nav-text">Node.js实现静态网站功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参数传递与获取"><span class="nav-number">1.10.</span> <span class="nav-text">参数传递与获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态网站开发"><span class="nav-number">1.11.</span> <span class="nav-text">动态网站开发</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模板引擎"><span class="nav-number">1.12.</span> <span class="nav-text">模板引擎</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express基本使用"><span class="nav-number">1.13.</span> <span class="nav-text">Express基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express基本介绍和使用"><span class="nav-number">1.14.</span> <span class="nav-text">Express基本介绍和使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express路由处理"><span class="nav-number">1.15.</span> <span class="nav-text">Express路由处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express中间件"><span class="nav-number">1.16.</span> <span class="nav-text">Express中间件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#初始"><span class="nav-number">1.16.1.</span> <span class="nav-text">初始</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#中间件"><span class="nav-number">1.16.2.</span> <span class="nav-text">中间件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参数"><span class="nav-number">1.16.3.</span> <span class="nav-text">参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模板"><span class="nav-number">1.16.4.</span> <span class="nav-text">模板</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express图书管理案例"><span class="nav-number">1.17.</span> <span class="nav-text">Express图书管理案例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Node-js操作数据库"><span class="nav-number">1.18.</span> <span class="nav-text">Node.js操作数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql第三方包基本使用"><span class="nav-number">1.19.</span> <span class="nav-text">mysql第三方包基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基于数据库实现登录功能"><span class="nav-number">1.20.</span> <span class="nav-text">基于数据库实现登录功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Express整合数据库实现增删改查业务"><span class="nav-number">1.21.</span> <span class="nav-text">Express整合数据库实现增删改查业务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后台API开发"><span class="nav-number">1.22.</span> <span class="nav-text">后台API开发</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#图书管理系统整合数据库"><span class="nav-number">1.23.</span> <span class="nav-text">图书管理系统整合数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后台接口开发"><span class="nav-number">1.24.</span> <span class="nav-text">后台接口开发</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基于Ajax与后台接口的前端渲染案例"><span class="nav-number">1.25.</span> <span class="nav-text">基于Ajax与后台接口的前端渲染案例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#从服务器主动发送请求"><span class="nav-number">1.26.</span> <span class="nav-text">从服务器主动发送请求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#调用第三方接口"><span class="nav-number">1.27.</span> <span class="nav-text">调用第三方接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Node-js网络爬虫"><span class="nav-number">1.28.</span> <span class="nav-text">Node.js网络爬虫</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="heweiliang"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">heweiliang</p>
  <div class="site-description" itemprop="description">heweiliang blog</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-google"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-stack-overflow"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title2</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title3</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heweiliang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">376k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:42</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"left","width":300,"height":600},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
